package leetcode.code0963;

import java.util.Arrays;

import leetcode.IDebug;

public class Solution implements IDebug {

	public double minAreaFreeRect(int[][] points) {
		double ans = 0;
		for (int i = 0; i < points.length; i++) {
			for (int j = i + 1; j < points.length; j++) {
				for (int m = j + 1; m < points.length; m++) {
					for (int n = m + 1; n < points.length; n++) {
						if (this.validSquare(points[i], points[j], points[m], points[n])) {
							long[] arr = new long[3];
							arr[0] = line(points[i], points[j]);
							arr[1] = line(points[i], points[m]);
							arr[2] = line(points[i], points[n]);
							Arrays.sort(arr);
							double cur = Math.sqrt(arr[0]) * Math.sqrt(arr[1]);
							ans = ans == 0 ? cur : Math.min(ans, cur);
						}
					}
				}
			}
		}
		return ans;
	}

	// 通过四个点，验证是否是矩形
	public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {
		return this.line2line(p1, p2, p3, p4) && this.line2line(p1, p3, p2, p4) && this.line2line(p1, p4, p2, p3);
	}

	private boolean line2line(int[] p1, int[] p2, int[] p3, int[] p4) {
		long dist1 = line(p1, p2);
		long dist2 = line(p3, p4);
		return dist1 == 0 || dist2 == 0 ? false : dist1 == dist2;
	}

	private long line(int[] p1, int[] p2) {
		long dist1 = Math.abs(p1[0] - p2[0]);
		long dist2 = Math.abs(p1[1] - p2[1]);
		return dist1 * dist1 + dist2 * dist2;
	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
